package com.cg.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cg.config.TokenFactory;
import com.cg.pojo.TbWeChatUser;
import com.cg.service.HelloService;
import com.cg.service.TbWeChatUserService;
import com.cg.util.JSSDKUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @Author: cg
 * @Date: Create IN 2017/6/21 10:20
 * @Email: cg_666666@163.com
 */
@Controller
@RequestMapping(value="/test")
public class testController {
    @Value("${cgdomain}")
    private String cgdomain;
    @Value("${wechat.mp.appId}")
    private String appid;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Reference(version = "1.0.0")
   private HelloService helloService;
    @Reference(version="1.0.0")
    private TbWeChatUserService tbWeChatUserService;
    @RequestMapping(value="/index")
    public String index(Model model){
        model.addAttribute("error","test");
        return "index";
    }
    @RequestMapping(value="/getToken")
    public String getToken(Model model){
        String token = TokenFactory.getAccessToken(stringRedisTemplate);
        model.addAttribute("error",token);
        return "index";
    }
    @RequestMapping(value="/getById")
    @ResponseBody
    public TbWeChatUser getById(String id){
        TbWeChatUser tbWeChatUser = tbWeChatUserService.findbyOpenid(id);
        return tbWeChatUser;
    }
    @RequestMapping(value="/testdubbo")
    @ResponseBody
    public String testdubbo(String url){
        String s = helloService.sayHello(url);
        return s;
    }
    @RequestMapping(value="/jssdk")
    public String testjssdk(Model model,String name){
        String jsApiTicket = TokenFactory.getJsApiTicket(stringRedisTemplate);
        Map<String, String> map = JSSDKUtil.sign(jsApiTicket, cgdomain + "/test/jssdk?name=" + name);
        model.addAttribute("error","测试");
        model.addAttribute("appId", appid);
        model.addAttribute("timestamp", map.get("timestamp"));
        model.addAttribute("nonceStr", map.get("nonceStr"));
        model.addAttribute("signature", map.get("signature"));

        String cardApiTicket = TokenFactory.getcardApiTicket(stringRedisTemplate);
        Map<String, String> cardsign = JSSDKUtil.cardsign(map.get("nonceStr"),map.get("timestamp"),cardApiTicket, "CASH",  "pCV5PwYv40glhO7QvfDNzwXdp8MM", appid);
        model.addAttribute("cardType",cardsign.get("cardType"));
        model.addAttribute("cardId",cardsign.get("cardId"));
        model.addAttribute("timestamp",cardsign.get("timestamp"));
        model.addAttribute("nonceStr",cardsign.get("nonceStr"));
        model.addAttribute("cardSign",cardsign.get("cardSign"));
        return "index";
    }
}
